<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="业务场景" prop="businessType">
        <el-select v-model="queryParams.businessType" placeholder="请选择业务场景" clearable size="small">
          <el-option
            v-for="dict in businessTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="文件上传名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件上传名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件扩展名" prop="fileExt">
        <el-input style="width: 150px"
                  v-model="queryParams.fileExt"
                  placeholder="请输入文件扩展名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传用户" prop="saveNickName">
        <el-input style="width: 150px"
                  v-model="queryParams.saveNickName"
                  placeholder="请输入上传用户用户名"
                  clearable
                  size="small"
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          plain
          v-hasPermi="['system:attachment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          plain
          @click="handleUpdate"
          v-hasPermi="['system:attachment:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          plain
          @click="handleDelete"
          v-hasPermi="['system:attachment:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attachmentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键" align="center" prop="id" v-if="false"/>
      <el-table-column label="业务场景" align="center" prop="businessType" :formatter="businessTypeFormat"/>
      <el-table-column label="文件名称" align="center" prop="fileName">
        <template slot-scope="{row}">
          <el-link type="primary" :href="serverUrl+row.downloadPath" target="_blank">
            {{ row.fileName }}
          </el-link>
        </template>
      </el-table-column>
      <!--<el-table-column label="文件存储名称" align="center" prop="realName"/>-->
      <!--<el-table-column label="文件路径" align="center" prop="filePath">-->
      <!--  <template slot-scope="{row}">-->
      <!--    <el-link type="primary" :href="serverUrl+row.downloadPath" target="_blank">-->
      <!--      {{ row.filePath }}-->
      <!--    </el-link>-->
      <!--  </template>-->
      <!--</el-table-column>-->
      <!--<el-table-column label="文件扩展名" align="center" prop="fileExt"/>-->
      <el-table-column label="描述" align="center" prop="remark"/>
      <el-table-column label="上传时间" align="center" prop="saveTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.saveTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="上传用户" align="center" prop="saveNickName"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:attachment:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:attachment:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改附件管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" v-loading="upload.isUploading">
        <el-form-item label="业务场景" prop="businessType">
          <el-select v-model="form.businessType" placeholder="请选择业务场景" style="width: 100%">
            <el-option
              v-for="dict in businessTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="附件上传" prop="attachment" v-if="!form.id">
          <el-upload class="upload-demo" drag
                     ref="upload" :limit="1"
                     :headers="upload.headers"
                     :data="uploadData"
                     :on-progress="handleFileUploadProgress"
                     :on-success="handleFileSuccess"
                     :multiple="false"
                     :auto-upload="false"
                     :action="upload.url"
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
          </el-upload>
        </el-form-item>
        <template v-if="form.id">
          <el-form-item label="文件上传名称">
            <el-input v-model="form.fileName" placeholder="请输入内容" disabled/>
          </el-form-item>
          <el-form-item label="文件存储名称">
            <el-input v-model="form.realName" placeholder="请输入内容" disabled/>
          </el-form-item>
          <el-form-item label="文件路径">
            <el-input v-model="form.filePath" placeholder="请输入内容" disabled/>
          </el-form-item>
        </template>
        <el-form-item label="附件描述" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAttachment,
  getAttachment,
  delAttachment,
  saveAttachment,
  exportAttachment
} from "@/api/system/attachment";
import {getToken} from "@/utils/auth";

export default {
  name: "Attachment",
  components: {},
  data() {
    return {
      serverUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 附件管理表格数据
      attachmentList: [],
      // 业务场景字典
      businessTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessType: undefined,
        fileName: undefined,
        fileExt: undefined,
        saveNickName: undefined
      },
      // 表单参数
      form: {},
      // 上传
      upload: {
        //是否正在上传
        isUploading: false,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/attachment/uploadAttachment"
      },
      // 表单校验
      rules: {
        businessType: [
          {required: true, message: "业务场景不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.getDicts("sys_attachment_business").then(response => {
      this.businessTypeOptions = response.data;
    });
    this.getList();
  },
  computed: {
    uploadData() {
      let d = {
        businessType: this.form.businessType,
        remark: this.form.remark || ''
      };
      if (this.form.id) {
        d.id = this.form.id;
      }
      return d;
    }
  },
  methods: {
    /** 查询附件管理列表 */
    getList() {
      this.loading = true;
      listAttachment(this.queryParams).then(response => {
        this.attachmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 业务场景字典翻译
    businessTypeFormat(row, column) {
      return this.selectDictLabel(this.businessTypeOptions, row.businessType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        businessType: undefined,
        fileName: undefined,
        realName: undefined,
        filePath: undefined,
        fileExt: undefined,
        remark: undefined,
        saveTime: undefined,
        saveUser: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加附件管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAttachment(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改附件管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //上传
          if (!this.form.id) {
            this.$refs['upload'].submit();
          } else {
            delete this.form.params;
            saveAttachment(this.form).then(res => {
              this.open = false;
              this.$modal.msgShow(res);
              this.getList();
            })
          }
        }
      });
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$modal.msgShow(response);
      this.getList();
      this.open = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除附件管理编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delAttachment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有附件管理数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportAttachment(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
      })
    }
  }
};
</script>
